/**
 * Created by charsle.
 * Date: 2017/5/24
 */
/**
 * 设置cookie
 */
let cookie= require('js-cookie');
function setCookie( cname, result ) {
  cookie.set(cname,result,{ expires: 1 })
  //return sessionStorage.setItem( cname, result )
}
/**
 * 获取cookie
 */
function getCookie( cname ) {
  return cookie.getJSON(cname);
}
/**
 * 删除cookie
 */
function delCookie( cname ) {
  cookie.remove(cname);
}

function Trim( str ) {
  console.log( str );
  return str.replace( /(^\s*)|(\s*$)/g, '' );
}
function tirmValidate( rule, value, callback ) {
  if ( !value.replace( /(^\s*)|(\s*$)/g, "" ) ) {
    return callback( new Error( '您不能能输入空格' ) );
  } else {
    callback();
  }
}

let reg = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;
function checkHttp( rule, value, callback ) {
  if ( value==='' ) {
    return callback( new Error( '地址不能为空' ) );
  }
  setTimeout( () => {
    if ( !reg.test( value ) ) {
      callback( new Error( '请输入正确的url地址' ) );
    } else {
      callback();
    }
  }, 100 );
}


function checkMobile ( rule, value, callback ) {
  let reg = /^1[3|4|5|7|8]\d{9}$/;

  if ( !value ) {
    return callback( new Error( '手机号码不能为空' ) );
  }
  setTimeout( () => {
    if ( !reg.test( value ) ) {
      callback( new Error( '请输入合法的手机号码' ) );
    } else {
      callback();
    }
  }, 100 );

}
function checkUserNum( rule, value, callback ){
  let reg = /^[1-9]\d{4}$/;

  if ( !value ) {
    return callback( new Error( '编号码不能为空' ) );
  }
  setTimeout( () => {
    if ( !reg.test( value ) ) {
      callback( new Error( '请输入合法的编号,如:10001' ) );
    } else {
      callback();
    }
  }, 100 );

}
function checkSoftSize( rule, value, callback ){
  if ( !value ) {
    return callback( new Error( '软件大小不能为0' ) );
  }
  setTimeout( () => {
    if ( !(/^\+?[1-9][0-9]*$/).test( value ) ) {
      callback( new Error( '请输入大于0的数字' ) );
    } else {
      callback();
    }
  }, 100 );
}

function checkMoney ( rule, value, callback ) {
  if ( !value ) {
    return callback( new Error( '数值不能为0' ) );
  }
  setTimeout( () => {
    if ( !(/^\+?[1-9][0-9]*$/).test( value ) ) {
      callback( new Error( '请输入数字值' ) );
    } else {
      callback();
    }
  }, 100 );
}
function validateMailbox ( rule, value, callback ) {
  setTimeout( () => {
    let isMail = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
    if ( !isMail.test( value ) ) {
      callback( new Error( '请输入正确格式的邮箱' ) );
    } else {
      callback();
    }
  }, 100 );
}
function validatePassword ( rule, value, callback ) {
  setTimeout( () => {
    let isPassword = /^[a-zA-Z0-9]{6,21}$/;
    if ( !isPassword.test( value ) ) {
      callback( new Error( '请输入6-21为数字和字母！' ) );
    } else {
      callback();
    }
  }, 100 );
}
function validateNumber ( rule, value, callback ) {
  setTimeout( () => {
    let isMail = /^[0-9]+$/;
    if ( !isMail.test( value ) ) {
      callback( new Error( '请输入数字' ) );
    } else {
      callback();
    }
  }, 100 );
}
function hasClass(ele, cls) {
  console.log(ele.className)
  return ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
}
//为指定的dom元素添加样式
function addClass(ele, cls) {
  if (!this.hasClass(ele, cls)) ele.className += " " + cls;
}
//删除指定dom元素的样式
function removeClass(ele, cls) {
  if (hasClass(ele, cls)) {
      var reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
      ele.className = ele.className.replace(reg, " ");
  }
}

const Rexports = {
  setCookie,
  getCookie,
  delCookie,
  Trim,
  tirmValidate,
  checkHttp,
  checkMobile,
  checkUserNum,
  checkSoftSize,
  checkMoney,
  validateMailbox,
  validatePassword,
  validateNumber,
  hasClass,
  removeClass,
  addClass
};
module.exports = Rexports;
